package com.yuanduan.mplus.view;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cj.framework.app.BaseActivity;
import com.cj.framework.util.HttpImp;
import com.cj.framework.util.IHttp;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;
import com.yuanduan.mplus.model.DBManager;
import com.yuanduan.mplus.model.entity.PointEntity;
import com.yuanduan.mplus.model.entity.User;
import com.yuanduan.mplus.presenter.PointListViewAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 积分，需修改
 */
public class PointActivity extends BaseActivity implements View.OnClickListener {
    private Toolbar toolbar;
    private TextView tv_currentPoint;
    private ListView lv_pointDetail;
    private PointListViewAdapter pointListViewAdapter;
    private List<PointEntity> lists=new ArrayList<PointEntity>();
    private User user;

//    private int num=0;
//    private int mountNum=2;

    private IHttp iHttp=new HttpImp();
    private IHttp.HttpInfoCallbackWithIndex httpInfoCallbackWithIndex=new IHttp.HttpInfoCallbackWithIndex() {
        @Override
        public void onSucceed(Object result, int index) {
            switch (index){
                case 0:
                    dealWithCurrentPoint(result);
                    break;
//                case 1:
//                    dealWithPointDetail(result);
//                    break;
            }
        }

        @Override
        public void onFailed(Object result, int index) {

        }
    };

    @Override
    protected void findId() {
        toolbar= (Toolbar) findViewById(R.id.toolbar);
        tv_currentPoint= (TextView) findViewById(R.id.point_tv_currentPoint);
        lv_pointDetail= (ListView) findViewById(R.id.point_lv_detail);
    }

    @Override
    protected void logic() {
        user= DBManager.getInstance(getApplicationContext()).getUser();

        toolbar.setTitle("我的积分");
        toolbar.setNavigationIcon(R.mipmap.back_arrow_black);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(this);

        pointListViewAdapter=new PointListViewAdapter(this);
        lv_pointDetail.setAdapter(pointListViewAdapter);


        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("UserPhone",user.getUserPhone());
        hashMap.put("UserPassword",user.getUserPwd());
        hashMap.put("TargetUserPhone",user.getUserPhone());
        hashMap.put("Page",1+"");
        hashMap.put("Count",50+"");
        hashMap.put("Sorting",-1+"");
        iHttp.httpInfoPostWithIndex(Config.URL+Config.USERINTEGRAL,Config.translateMap(hashMap),httpInfoCallbackWithIndex,0);
//        iHttp.httpInfoPostWithIndex(Config.URL,new HashMap<String, String>(),httpInfoCallbackWithIndex,1);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_point;
    }


    @Override
    public void onClick(View v) {
        this.finish();
    }

    public void dealWithCurrentPoint(Object result){
        //设置当前积分
        if(TextUtils.isEmpty((CharSequence) result)){
            return;
        }
        try {
            JSONObject data=new JSONObject((String) result);
            if(data.getInt("RS")!=1){
                Toast.makeText(PointActivity.this,"积分获取失败",Toast.LENGTH_SHORT).show();
            }else{
                tv_currentPoint.setText(data.getInt("TotalIntegral"));
                JSONArray list=data.getJSONArray("IntegralList");
                for(int i=0;i<list.length();i++){
                    JSONObject o1=list.getJSONObject(i);
                    PointEntity entity=new PointEntity();
                    entity.setTitle(o1.getString("Title"));
                    entity.setTime(o1.getString("Timer")+"000");
                    entity.setAddedPoint(o1.getInt("Integral")+"");
                    lists.add(entity);
                }
                pointListViewAdapter.setDatalists(lists);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        tv_currentPoint.setText("1000");
    }

    private void dealWithPointDetail(Object result) {
        //设置listview详情
        PointEntity entity=new PointEntity();
        entity.setAddedPoint("+10");
        entity.setTime("2016-6-14 15:52");
        lists.add(entity);
        PointEntity entity1=new PointEntity();
        entity1.setAddedPoint("+20");
        entity1.setTime("2016-6-14 16:52");
        lists.add(entity1);
        pointListViewAdapter.setDatalists(lists);
        lists.clear();
    }
}
